<?php

namespace App\Http\Controllers\Passport;

use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PassportController extends Controller
{
    //

    public function login(Request $request)
    {
        if (\Auth::attempt($request->input())) {
            $user = \Auth::user();

            $successData = [
                'token' => $user->createToken('App')->token->expires_at,
                'name' => $user->name,
//                'ext' => $user->clients->expires_at
            ];

            return [
                'code' => 200,
                'data' => $successData,
            ];
        } else {
            return ['code' => 401, 'message' => '用户名或密码错误'];
        }
    }

    public function register(Request $request)
    {
        $postData = $request->input();
        $saveData = [
            'name' => $postData['name'],
            'email' => $postData['email'],
            'password' => bcrypt($postData['password'])
        ];
        $user = User::query()->create($saveData);

        $token = $user->createToken('app')->accessToken;
        $name = $user->name;
        $successData = [
            'token' => $token,
            'name' => $name
        ];

        return ['code' => 200, 'data' => $successData];
    }
}
